﻿/*
	Class			Tools
	
	Description		A class of general static (very useful) functions.
					
	History			1.00	6/29/07		Created
	
	To Do			Fix the removeOverflow to remove spaces preceding "..."
					Split into Categories? ie Position, TextField etc.
					create functions for stacking all the objects in a DisplayObjectContainer.
*/

package utilities
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Rectangle;
	
	public class Tools
	{
		// Position Stuff
		public static function center ($object:DisplayObject, ...args):void
		{
			var offsetX:Number;
			var offsetY:Number;
			var centerX:Number;
			var centerY:Number;
			
			if (args[0] is Rectangle)
			{
				var coordinateSpace:Rectangle = args[0];
				
				offsetX = coordinateSpace.x;
				offsetY = coordinateSpace.y;
				centerX = (coordinateSpace.width / 2) - ($object.width / 2);
				centerY = (coordinateSpace.height / 2) - ($object.height / 2);
			}
			
			else if (args[0] is DisplayObject)
			{
				var relativeToObject:DisplayObject = args[0];
				var objectBounds:Rectangle         = relativeToObject.getBounds (relativeToObject);
				var relativeBounds:Rectangle       = $object.getBounds (relativeToObject);
				
				var objectX:Number   = objectBounds.x * relativeToObject.scaleX;
				var objectY:Number   = objectBounds.y * relativeToObject.scaleY;
				var relativeX:Number = relativeBounds.x * relativeToObject.scaleX;
				var relativeY:Number = relativeBounds.y * relativeToObject.scaleY;
				
				offsetX = $object.x + objectX - relativeX;
				offsetY = $object.y + objectY - relativeY;
				centerX = (relativeToObject.width / 2) - ($object.width / 2);
				centerY = (relativeToObject.height / 2) - ($object.height / 2);
				
			}
			
			else if (args[0] is Number && args[1] is Number)
			{
				var relativeWidth:Number  = args[0];
				var relativeHeight:Number = args[1];
				
				offsetX = 0;
				offsetY = 0;
				centerX = (relativeWidth / 2) - ($object.width / 2);
				centerY = (relativeHeight / 2) - ($object.height / 2);
			}
			
			$object.x = Math.round (offsetX + centerX);
			$object.y = Math.round (offsetY + centerY);
		}
		
		
		public static function stackVertical ($objects:Array, $spacing:Number = 0):void
		{
			for (var i in $objects)
			{
				if (i != 0)
				{
					var previousY:Number      = $objects[i-1].y;
					var previousHeight:Number = $objects[i-1].height;
					
					$objects[i].y = previousY + previousHeight + $spacing;
				}
			}
		}
		
		
		public static function stackVerticalIn ($object:DisplayObjectContainer, $spacing:Number = 0):void
		{
			var objectsArray:Array = new Array ();
			
			for (var i:int = 0; i < $object.numChildren; i++)
			{
				var objectToAdd:DisplayObject = $object.getChildAt (i);
				objectsArray.push (objectToAdd);
			}
			
			Tools.stackVertical (objectsArray, $spacing);
		}
		
		
		public static function stackHorizontal ($objects:Array, $spacing:Number = 0):void
		{
			for (var i in $objects)
			{
				if (i != 0)
				{
					var previousX:Number     = $objects[i-1].x;
					var previousWidth:Number = $objects[i-1].width;
					
					$objects[i].x = previousX + previousWidth + $spacing;
				}
			}
		}
		
		
		public static function stackHorizontalIn ($object:DisplayObjectContainer, $spacing:Number = 0):void
		{
			var objectsArray:Array = new Array ();
			
			for (var i:int = 0; i < $object.numChildren; i++)
			{
				var objectToAdd:DisplayObject = $object.getChildAt (i);
				objectsArray.push (objectToAdd);
			}
			
			Tools.stackHorizontal (objectsArray, $spacing);
		}
		
		
		// TextField Stuff
		public static function hasOverflow ($textField:Object):Boolean
		{
			return $textField.maxScrollH > 1;
		}
		
		
		public static function removeOverflow ($textField:Object, $omissionIndicator:String = "..."):void
		{
			if (!Tools.hasOverflow ($textField))
				return;
			
			var omissionCharacters:Array = $omissionIndicator.split ("");
			var characters:Array         = $textField.text.split ("").concat (omissionCharacters);
			var deleteCharacter:int      = - (omissionCharacters.length + 1);
			
			$textField.text = characters.join ("");
			
			while (Tools.hasOverflow ($textField))
			{
				characters.splice (deleteCharacter, 1);
				$textField.text = characters.join ("");
			}
		}
	}
}